package com.lsfb.signsys.login;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.common.base.Strings;
import com.lsfb.signsys.BaseActivity;
import com.lsfb.signsys.Main2Activity;
import com.lsfb.signsys.R;
import com.lsfb.signsys.forget.ForgetActivtiy;
import com.lsfb.signsys.main.MainActivtiy;

import butterknife.BindView;
import butterknife.OnClick;

import static com.google.common.base.Preconditions.checkNotNull;

//import static com.google.common.base.Preconditions.checkNotNull;


/**
 * Created by zgscwjm on 2018/3/15.
 */

public class LoginActivity extends BaseActivity implements LoginContract.View {

    /**
     * 这个应该是activity 保存到 saveInstanceState里面的标识符
     */
    private static final String CURRENT_FILTERING_KEY = "CURRENT_FILTERING_KEY";
    /**
     * 设置Presenter层
     */
    private LoginPresenter mLoginPresenter;

    /**
     * 设置Presenter 接口对象.
     */
    private LoginContract.Presenter mPresenter;

    @BindView(R.id.act_login_btn_login)
     Button btn_login;

    @BindView(R.id.act_login_et_username)
     TextView tv_username;

    @BindView(R.id.act_login_et_password)
     TextView tv_password;

    @Override
    public int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {
        mLoginPresenter = new LoginPresenter(this);
    }


    /**
     * 检查是否拥有指定的所有权限
     */
    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                // 只要有一个权限没有被授予, 则直接返回 false
                return false;
            }
        }
        return true;
    }

    public void permission(){
        String per[]=new String[]{Manifest.permission.INTERNET};
        if (!checkPermissionAllGranted(per)) {
            ActivityCompat.requestPermissions(this,per, 100);
        }
    }
    @OnClick({R.id.act_login_btn_login, R.id.act_login_tv_forget})
    public void onClick(View view) {
        permission();
        switch (view.getId()) {
            case R.id.act_login_btn_login:

                String username =tv_username.getText().toString();
                String password = tv_password.getText().toString();
                mLoginPresenter.login(username,password);
                break;
            case R.id.act_login_tv_forget:
                mLoginPresenter.forget();
                break;
            default:
                break;
        }
    }
//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        setContentView(R.layout.activity_login);
//
//        mLoginPresenter = new LoginPresenter(this);
//
//        btn_login.setOnClickListener(__->mLoginPresenter.login());
//
//        if (savedInstanceState != null){
//            //todo 填充保存的数据
//        }
//    }


    @Override
    public void setPresenter(@NonNull LoginContract.Presenter persenter) {
        this.mPresenter = checkNotNull(persenter);
    }

    @Override
    public void showLoad() {

    }

    @Override
    public void endLoad() {

    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMainActive() {
        startActivity(new Intent(LoginActivity.this, MainActivtiy.class));
        finish();
    }

    @Override
    public void showForgetPwdActive() {
        startActivity(new Intent(LoginActivity.this, Main2Activity.class));
    }


}
